<?php
namespace app\admin\controller;

use think\facade\View;
use app\Request;
use app\repositories\contracts\ArticleInterface;

class Article extends ViewBaseController
{
    public function index()
    {
        return View::fetch('index');
    }

    public function getArticleListJson(Request $request,ArticleInterface $article)
    {
        $page = $request->param('page');
        $limit = $request->param('limit');
        return layuiJson(0,'文章列表获取成功',$article->getArticleListPage($page,$limit,'id','asc'),
            $article->getArticleListCount());
    }

    public function searchArticleListJson(Request $request,ArticleInterface $article)
    {
//        halt($request->param());
        $page = $request->param('page');
        $limit = $request->param('limit');
        $searchField = $request->param('searchField');
        $searchWhere = $request->param('searchWhere');
        $searchData = '%'.$request->param('searchData').'%';
        return layuiJson(0,'文章列表获取成功',$article->searchArticleListPage($searchField,$searchWhere,$searchData,$page,
            $limit,'id','asc'),
            $article->getArticleListCount());
    }

    /***
     * @param ArticleInterface $article
     * @return \think\response\Json
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 9:21
     * @Description 删除文章接口
     */
    public function del(Request $request,ArticleInterface $article)
    {
        $idArray = $request->param('idArray');
        $bool = $article->delArticleList($idArray);
        if(!$bool)
        {
            $result = layuiJson(1,'删除失败');
            return $result;
        }
        return layuiJson(0,'删除成功',$idArray);
    }

    /***
     * @param Request $request
     * @param ArticleInterface $article
     * @return \think\response\Json
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 10:40
     * @Description 修改指定字段接口
     */
    public function editList(Request $request,ArticleInterface $article)
    {
        $editArray = $request->param();

        $result = $article->editArticleList($editArray);
        if(!$result)
        {
            $result = layuiJson(1,'修改失败');
            return $result;
        }
        return layuiJson(0,'修改成功',$result);
    }

    /***
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 13:46
     * @Description 添加文章
     */
    public function add(Request $request,ArticleInterface $article)
    {
        if(!$request->isPost()){
            $allArticleList = $article->getArticleList();
            return View::fetch('',['allArticleList'=>$allArticleList]);
        }

        $addArray = $request->param();
        $author = $request->rootData;
        //  ^ array:11 [
        //  "id" => 1
        //  "root_name" => "admin"
        //  "root_password" => "cabbff0357dab22b881e5ba21fc1732f"
        //  "group_id" => 1
        //  "salt" => "etLPxY"
        //  "avatar_src" => "/storage/topic/65\d79362f430a9d7146d6ca7e3d47fa6216ad8f1.bmp"
        //  "status" => 1
        //  "email" => "547966838@qq.com"
        //  "mobile" => "18286199956"
        //  "create_time" => "2021-12-15 15:46:16"
        //  "update_time" => "2022-06-23 04:14:51"
        //]
        $type = $author['group_id'];
        $result = $article->addArticleList($addArray,$author,$type);
        if(!$result){
            return layuiJson(1,'添加失败',$result);
        }

        return layuiJson(0,'添加成功',$result);
    }

    /***
     * @Author Vee7
     * @Date 2021/12/16
     * @Time 10:20
     * @Description 会员组权限管理页面
     */
    public function auth(Request $request,ArticleInterface $article,MenuInterface $menu)
    {
        $id = $request->param('id');
        if(!$id){
            return redirect('/admin/User_group/index');
        }
        if(!$request->isPost()){
            $ArticleData = $article->getArticleData($id);
            $MenuListData = $menu->getListForAuth($id);
            return View::fetch('',['ArticleData'=>$ArticleData,'MenuListData'=>json_encode($MenuListData)]);
        }

        $addArray = $request->param();
        $result = $article->addArticleList($addArray);
        if(!$result){
            return layuiJson(1,'添加失败',$result);
        }

        return layuiJson(0,'添加成功',$result);
    }

    /***
     * @Author Vee7
     * @Date 2021/12/17
     * @Time 10:02
     * @Description 无访问权限页面
     */
    public function NoPromise($errorMsg="非法访问")
    {

        return View::fetch('nopromise',['errorMsg'=>$errorMsg]);
    }
}
